import os
from utils import doc_util, read_config, parse_util
from service import get_a_stock_info
from database.mapper import stock_info

common_path = os.path.join(read_config.data_path, 'common')

stock_dict = doc_util.get_a_doc_dict(os.path.join(common_path, 'config', '0.股票信息'), 'code')
macd_dict = doc_util.get_a_doc_dict(os.path.join(common_path, 'macd', '000000'), 'code')


# 获取股票基本信息
def stock(code):
    global stock_dict
    if code not in stock_dict.keys():
        stock_dict = doc_util.get_a_doc_dict(os.path.join(common_path, 'config', '0.股票信息'), 'code')
    if code not in stock_dict.keys():
        s_info = stock_info.selectone(code)
        s_info['exchange'] = str(s_info['exchange'])
        stock_dict[code] = s_info
        if len(s_info) == 0:
            stock_dict[code] = get_a_stock_info.add_a_stock(code)[code]
        doc_util.gen_a_doc(os.path.join(common_path, 'config'), '0.股票信息', stock_dict)
    return stock_dict[code]


# 获取股票基本信息
def get_stock_cache():
    global stock_dict
    if len(stock_dict) == 0:
        stock_dict = doc_util.get_a_doc_dict(os.path.join(common_path, 'config', '0.股票信息'), 'code')
    return stock_dict


# 获取股票基本信息
def new_stock_all():
    global stock_dict
    if len(stock_dict) == 0:
        stock_dict = doc_util.get_a_doc_dict(os.path.join(common_path, 'config', '0.股票信息'), 'code')
    result = {}
    for key in stock_dict.keys():
        result[key] = stock_dict[key]
    return result


def init_stock_dict():
    s_list = parse_util.list_to_dict(stock_info.selectall(), 'code')
    doc_util.gen_a_doc(os.path.join(common_path, 'config'), '0.股票信息', s_list)


if __name__ == '__main__':
    init_stock_dict()
    print("end")
